package com.konjac.controller;

import com.konjac.dto.SaleOrderDto;
import com.konjac.pojo.BookInfo;
import com.konjac.pojo.ReturnOrder;
import com.konjac.pojo.SaleOrder;
import com.konjac.pojo.ServiceState;
import com.konjac.service.impl.AddOrderServiceImpl;
import com.konjac.service.impl.ReturnOrderServiceImpl;
import com.konjac.service.impl.SaleOrderServiceImpl;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
public class CustomerController {
    @RequestMapping("/showBookInfo")
    @ResponseBody
    public List<BookInfo> showBookInfo() {
        BookInfo bookInfo = new BookInfo();
        AddOrderServiceImpl addOrderService = new AddOrderServiceImpl();
        List<BookInfo> bookInfoList = null;
        try {
            bookInfoList = addOrderService.showBookInfo();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bookInfoList;
    }

    @RequestMapping("/showSaleOrder")
    @ResponseBody
    public List<SaleOrder> showSaleOrder() {
        SaleOrder saleOrder = new SaleOrder();
        List<SaleOrder> saleOrderList = null;
        ReturnOrderServiceImpl returnOrderService = new ReturnOrderServiceImpl();
        try {
            saleOrderList=returnOrderService.showSale();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return saleOrderList;
    }


    @RequestMapping("/buyBook")
    @ResponseBody
    public ServiceState saleBook(@RequestBody SaleOrderDto form) {
        ServiceState serviceState = new ServiceState();
        SaleOrderServiceImpl saleOrderService = new SaleOrderServiceImpl();
        System.out.println(form);
        try {
            serviceState = saleOrderService.sale(form.getBookId(), form.getUserName(), form.getBookNumber());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return serviceState;
    }

    @RequestMapping("/returnOrder")
    @ResponseBody
    public ServiceState returnOrder(@RequestBody ReturnOrder form) {

        ServiceState serviceState = new ServiceState();
        ReturnOrderServiceImpl returnOrderService = new ReturnOrderServiceImpl();
        try {
            serviceState = returnOrderService.returnOrder(form.getId());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return serviceState;
    }

}
